package com.example.movie.ui.home.adapter

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.movie.R
import com.example.movie.common.constants.AppConstants
import com.example.movie.data.movie.model.vo.MovieListBeanVo
import com.example.movie.ui.movie.MovieDetailActivity
import kotlinx.android.synthetic.main.item_movie_list.view.*

class MovieListAdapter(
    private val items: List<MovieListBeanVo>,
    private val context: Context
) :
    RecyclerView.Adapter<MovieListAdapter.MyViewHolder>() {

    class MyViewHolder(view: View) :
        RecyclerView.ViewHolder(view)

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): MyViewHolder {
        return MyViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_movie_list, parent, false)
        )
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.itemView.movie_name.text = items[position].movieName
        holder.itemView.action.setOnClickListener {
            it.findNavController().navigate(R.id.action_navigation_home_to_navigation_cinema)
        }
        Glide
            .with(context)
            .load(AppConstants.STATIC_SOURCE_URL + items[position].cimg)
            .placeholder(R.mipmap.logo)
            .error(R.mipmap.ic_launcher)
            .into(holder.itemView.movie_img)
        holder.itemView.movie_img.setOnClickListener {
            with(Bundle()){
                putInt("movieId", items[position].movieId)
                val intent = Intent(context, MovieDetailActivity::class.java)
                intent.putExtras(this)
                context.startActivity(intent)
            }
        }
    }

    override fun getItemCount() = items.size
}
